package lww231;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

//      k1      v1      k2      v2
public class MovieMapper extends Mapper<LongWritable, Text, Text, MovieInfo> {
    @Override
    protected void map(LongWritable k1, Text v1, Context context)
            throws IOException, InterruptedException{
        //数据：{"_id":{"$oid":"5ad89b396afaf881d81e6ce3"},"title":"快手枪手快枪手","year":"2016","type":"喜剧,动作","star":5.1,"director":"潘安子","actor":"林更新,张静初,腾格尔,刘晓庆,锦荣,曾江,施予斐,含笑,文淇","pp":16518,"time":115,"film_page":"https://movie.douban.com/subject/26219893/"}
        try {
            //转换器
            ObjectMapper objectMapper = new ObjectMapper();
            MovieInfo info = objectMapper.readValue(v1.toString(), MovieInfo.class);
            //"actor":"林更新,张静初,腾格尔,刘晓庆,锦荣,曾江,施予斐,含笑,文淇"
            String actor = info.getActor();
            //分词
            String[] person = actor.split(",");
            String input = "徐少强";
            for (int i=0; i<person.length; i++){
                if (input.equals(person[i])) {
                    context.write(new Text(person[i]), info);
                } else {
                    System.err.println(person);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
			e.printStackTrace();// 只能输出在控制台当中，日志文件看不到
        }
    }
}
